package v1

import (
	"gin_server/middleware"
	"gin_server/model"
	"gin_server/uitl/response"

	"github.com/gin-gonic/gin"
)

// 登陆
func Login(c *gin.Context) {
	var (
		data  model.User
		token string
		code  int
	)

	c.ShouldBindJSON(&data)
	code = model.LoginModel(data.Username, data.Password)
	if code == response.SUCCESS {
		token, code = middleware.SetToken(data.Username)
		if code == response.SUCCESS {
			data := map[string]string{
				"token": token,
			}
			response.ResMsgOk(c, data, "获取token成功")
		}
	} else {
		response.ResMsg(c, "用户名或密码错误")
	}

}

// GetUser 获取用户信息
func GetUser(c *gin.Context) {
	// c.JSON：返回JSON格式的数据
	//pageSize, _ := strconv.Atoi(c.Query("pagesize"))
	//pageNum, _ := strconv.Atoi(c.Query("pagenum"))
	//
	//if pageSize == 0 {
	//	pageSize = -1
	//}
	//if pageNum == 0 {
	//	pageNum = 2
	//}
	//data := model.GetUsersModel(pageSize, pageNum)
	response.ResMsg(c, "请求成功")
}

// 新增用户
func UserCreate(c *gin.Context) {
	var data model.User
	bErr := c.ShouldBindJSON(&data)
	if bErr != nil {
		code := model.IsUserModel(data.Username)
		if code == response.SUCCESS {
			model.CreateUserModel(&data)
			response.ResMsg(c, "新增用户成功")
		} else {
			response.ResMsg(c, "用户已存在")
		}
	} else {
		response.ResMsg(c, "填写数据有误,请检查")
	}
}

// DeleteUser 删除用户
//func DeleteUser(c *gin.Context) {
//	id, _ := strconv.Atoi(c.Param("id"))
//	code = model.DeleteUserModel(id)
//	if result == codemsg.SUCCESS {
//		codemsg.ResMsg(c, http.StatusOK, "用户删除成功")
//	} else {
//		codemsg.ResError(c, http.StatusOK, codemsg.ERROR_USER_NOT_EXIST)
//	}
//}

// UpdateUser 更新用户信息
//func UpdateUser(c *gin.Context) {
//	var data model.User
//	_ = c.ShouldBindJSON(&data)
//	code := model.IsUserModel(data.Username)
//	if code == codemsg.SUCCESS {
//		model.UpdateUserModel(data.ID, &data)
//		codemsg.ResMsg(c, http.StatusOK, "编辑用户成功!")
//	} else {
//		codemsg.ResError(c, http.StatusOK, codemsg.ERROR_USERNAME_USED)
//	}
//}
